package com.itheima.algorithm.dynamicprogramming;

/*
斐波那契动态规划
 */
public class Fibonacci {
    public static void main(String[] args) {
        System.out.println(fibonacci(6));
        System.out.println(fibonacci2(6));
    }

    // 基础数组法
    public static int fibonacci(int n){
        // 用来缓存数组
        int[] dp = new int[n+1];
        dp[0] = 0;
        dp[1] = 1;
        for(int i=2;i<=n;i++){
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }

    // 滚动递推法
    public static int fibonacci2(int n){
        if (n == 0){
            return 0;
        }
        if (n == 1){
            return 1;
        }
        int a= 0;
        int b =1;
        for (int i=2;i<=n;i++){
            int c = a + b;
            a = b;
            b = c;
        }
        return b;
    }

}
